package com.compass.doctor.ui.prescription.fragment

import android.os.Bundle
import android.view.View
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.compass.doctor.base.BaseVBFragment
import com.compass.doctor.callback.OnCustomButtonClick
import com.compass.doctor.databinding.FragmentBaseListBinding
import com.compass.doctor.ui.patient.WebRXExamineActivity
import com.compass.doctor.ui.prescription.adapter.RxExamineAdapter
import com.compass.doctor.ui.prescription.viewmodel.PrescribeViewModel
import com.compass.framework.constant.KEY_ORDER_TYPE
import com.compass.framework.constant.REFRESH_RX
import com.compass.framework.constant.REFRESH_RX_EXAMINE
import com.compass.framework.constant.RX_DETAILS
import com.compass.framework.decoration.NormalItemDecoration
import com.compass.framework.ext.gone
import com.compass.framework.ext.load
import com.compass.framework.ext.visible
import com.compass.framework.utils.LiveDataBus
import com.compass.framework.utils.dpToPx
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener
import com.scwang.smart.refresh.layout.listener.OnRefreshListener

/**
 * Description： 待审核处方--药师
 * Date:2024/2/21 14:40
 */
class RxToBeExamineFragment : BaseVBFragment<FragmentBaseListBinding>(), OnLoadMoreListener,
    OnRefreshListener {

    private val prescribeViewModel: PrescribeViewModel by viewModels()
    private lateinit var mAdapter: RxExamineAdapter
    private var mPage = 1
    private var orderType: Int = 1
    override fun initView(view: View, savedInstanceState: Bundle?) {
        arguments?.let {
            orderType = it.getInt(KEY_ORDER_TYPE)
        }
        mBinding?.llBottomBtn?.gone()
        initRecyclerView()
    }

    private fun initRecyclerView() {
        mBinding?.refreshLayout?.apply {
            setEnableRefresh(true)
            setEnableLoadMore(true)
            setOnRefreshListener(this@RxToBeExamineFragment)
            setOnLoadMoreListener(this@RxToBeExamineFragment)
        }
        mAdapter = RxExamineAdapter()
        val dp10 = dpToPx(10)
        mBinding?.recyclerView?.apply {
            layoutManager = LinearLayoutManager(context)
            addItemDecoration(NormalItemDecoration().apply {
                setBounds(left = dp10, top = dp10, right = dp10, bottom = dp10)
                setLastBottom(true)
            })
            adapter = mAdapter
        }


        mAdapter.setBottomCallBack(object : OnCustomButtonClick {
            override fun onButtonCallBack(position: Int, msgType: String?) {
                val item=mAdapter.getItem(position)
                item?.let {details->
                    WebRXExamineActivity.start(
                        context = requireActivity(),
                        url = RX_DETAILS +details.id,
                        title ="处方详情",
                        id=details.id,0
                    )
                }

            }
        })
    }


    override fun initData() {
        showLoading()
        getRxExamine()
        LiveDataBus.get().with(REFRESH_RX_EXAMINE, String::class.java).observe(this) {
            getRxExamine()
        }

        prescribeViewModel.prescriptionRecordLiveData.observe(this) {
            dismissLoading()
            if (mPage == 1) {
                mAdapter.setData(it)
                mBinding?.refreshLayout?.finishRefresh()
                mBinding?.refreshLayout?.setEnableLoadMore(load(it?.size))
                if (it.isNullOrEmpty()) {
                    mBinding?.emptyView?.visible()
                } else {
                    mBinding?.emptyView?.gone()
                }
            } else {
                mBinding?.refreshLayout?.finishLoadMore()
                if (!it.isNullOrEmpty()) {
                    mAdapter.addAll(it)
                } else {
                    mBinding?.refreshLayout?.setEnableLoadMore(false)
                }
            }
        }
    }


    private fun getRxExamine() {
        prescribeViewModel.getRxExamine(mPage,"1")
    }


    override fun onLoadMore(refreshLayout: RefreshLayout) {
        mPage++
        getRxExamine()
    }

    override fun onRefresh(refreshLayout: RefreshLayout) {
        mPage = 1
        getRxExamine()
    }
}